package com.example.JcGQS.service.impl;

import com.example.JcGQS.dao.CategoryDao;
import com.example.JcGQS.dao.HrmsDao;
import com.example.JcGQS.exception.CustomException;
import com.example.JcGQS.pojo.dto.DeleteCategoryDTO;
import com.example.JcGQS.pojo.dto.InsertCategoryDTO;
import com.example.JcGQS.pojo.dto.PageDTO;
import com.example.JcGQS.pojo.dto.UpdateCategoryDTO;
import com.example.JcGQS.pojo.entity.GoodsCategory;
import com.example.JcGQS.pojo.vo.DataRecordVo;
import com.example.JcGQS.response.ResultUtil;
import com.example.JcGQS.service.CategoryService;
import com.example.JcGQS.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 物品类型serviceimpl类
 * @author 末日
 * @date 2023/11/21
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
@Autowired
    CategoryDao categoryDao;
@Autowired
    HrmsDao hrmsDao;

    /**
     * 新增物品类型
     */
    @Override
    public void insertCategory(InsertCategoryDTO insertCategoryDTO, String token) {
       Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_administrator");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        GoodsCategory category = categoryDao.categorySelectName(insertCategoryDTO.getCategoryName());
        if (category==null){
             category= new GoodsCategory();
            BeanUtils.copyProperties(insertCategoryDTO,category);
            categoryDao.insertCategory(category);
        }else {
            throw new CustomException("该物品类型已存在");
        }


    }

    /**
     * 删除物品类型
     */
    @Override
    public void deleteCategory(DeleteCategoryDTO deleteCategoryDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_administrator");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        Integer integer1 = categoryDao.categorySelect(deleteCategoryDTO.getCategoryId());
        if (integer1!=0){
            categoryDao.deleteCategory(deleteCategoryDTO);
        }else {
            throw new CustomException("该物品类型不存在");
        }

    }

    /**
     * 修改物品类型
     */
    @Override
    public void updateCategory(UpdateCategoryDTO updateCategoryDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_administrator");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        Integer integer1 = categoryDao.categorySelect(updateCategoryDTO.getCategoryId());
        if (integer1!=0){
            GoodsCategory category = categoryDao.categorySelectName(updateCategoryDTO.getCategoryName());
            if (category==null){
                category = new GoodsCategory();
                BeanUtils.copyProperties(updateCategoryDTO,category);
                categoryDao.updateCategory(category);
            }else {
                throw new CustomException("该物品类型已存在");
            }

        }else {
            throw new CustomException("该物品类型不存在");
        }

    }

    /**
     * 物品类型列表
     */
    @Override
    public DataRecordVo<GoodsCategory> categoryList(PageDTO pageDTO, String token) {
        log.info("11");
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_administrator");
        if (integer == 0){
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<GoodsCategory> goodsCategories = categoryDao.categoryList();
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>(goodsCategories);
        return new DataRecordVo<>(pageInfo.getTotal(),pageInfo.getList());

    }
}
